// 自动检测任务与英雄属性

import Configer from "../frame/apiModel/config/Configer";
import {ConfigKey} from "../constant/ConfigConstant";
import {GlobalEnv} from "../utils/GlobalEnv";
import {GuiControllerOpt, GuiId} from "../controller/model/ControllerOpt";
import {GuiController, OptType} from "../controller/GuiController";
import {UnitController} from "../controller/UnitController";
import MathUtil from "../frame/codeAdapter/MathUtil";
import {ColorStr} from "../type/Type";
import {TaskController} from "../controller/TaskController";
import {TriggerFactory} from "../frame/TriggerFactory";
import {UnitModel} from "../frame/apiModel/adapterModel/model3/UnitModel";
import {PlayerModel} from "../frame/apiModel/adapterModel/model4/PlayerModel";
import {Logger} from "../frame/apiModel/config/Logger";
import {CodeUtil} from "../frame/codeAdapter/CodeUtil";
import {DataRouter} from "../frame/dataSource/DataRouter";
import {DataRouterUtil} from "../utils/router/DataRouterUtil";
import UnitUtil from "../utils/UnitUtil";
import {UnitDataKey} from "../frame/dataSource/DataSource";
import {TextGuiKey} from "../constant/UIConfig";
import {ResourceKey} from "../constant/ResourceConstant";

export function unitLevelUpTrigger() {
    const levelUpInterval = Configer.getConfig(ConfigKey.levelUpInterval) as number;
    const caluBack = function (this: void, activeUnit: UnitModel, activePlayer: PlayerModel, level: number, upLevel: number) {
        const isHero = GlobalEnv.isInHeroGroup(activeUnit);

        const strId = activeUnit?.strId
        const unitData = DataRouter.getUnitDataByUnitId(strId);
        if (unitData) {
            const growthAttrId = unitData.growthAttrId;
            const attr = DataRouterUtil.getAttrByAttrInterface(growthAttrId, upLevel);
            UnitUtil.addSubAttr(activeUnit, attr.unitAttr);
            UnitController.getInstance().addCaluAttributeTaskByUnit(activeUnit, false, '升级属性成长');
        }


        if (upLevel > 0) {
            activeUnit.data[UnitDataKey.maxLvHistory] = level;
        }

        const lvCache = activeUnit.data[UnitDataKey.maxLvCache]
        if (level > lvCache) {
            UnitController.getInstance().setHeroLvCache(activeUnit, level);
        } else {
            return;
        }

        if (isHero) {
            // 防作弊
            const tempLv = level - lvCache;
            const maxLevelUpNum = Configer.getConfig(ConfigKey.maxLevelUpNum);
            if (tempLv > maxLevelUpNum) {
                const playerName = activePlayer.name;
                let errText = `检测到玩家${playerName}疑似作弊！(作弊会失去游戏的乐趣，请勿作弊！)`;
                errText = CodeUtil.getColorStr(ColorStr.red, errText)
                Logger.toastProduct(errText)
                TaskController.getInstance().toLose(activePlayer);
            }

            if (!CodeUtil.isNullOrZero(levelUpInterval)) {

                if (level >= 30 && lvCache < 30) {
                    GuiController.getInstance().showUiText(ResourceKey.arousal, true, activePlayer);
                }

                const times = level / levelUpInterval;
                const timesInt = MathUtil.floor(times);

                const cacheTimes = lvCache / levelUpInterval;
                const cacheTimesInt = MathUtil.floor(cacheTimes);

                if (timesInt > cacheTimesInt) {
                    for (let i = 0; i < (timesInt - cacheTimesInt); i++) {
                        const request = { };
                        const opt = new GuiControllerOpt(GuiId.chooseAchBar, request);
                        GuiController.getInstance().showGui(opt, activePlayer, OptType.open);
                    }
                }
            }
        }
    }
    TriggerFactory.getInstance().registerUnitLevelUp(caluBack)
}